﻿using System;
using System.Linq;
using Evpo.ContactData.Data;
using Evpo.ContactData.Common;
using System.Collections.Generic;

namespace Evpo.ContactData.ViewModel
{
    public class ContactEditViewModel
    {
        public ContactEditViewModel(ModelFactory factory, IConnection conn)
        {
            _conn = (Connection)conn;
            _adapter = factory.CreateContactAdapter(conn);
            _titleAdapter = factory.CreateTitleAdapter(conn);
            _maritalStatusAdapter = new MaritalStatusAdapter(_conn);
            _homeTownAdapter = new HomeTownAdapter(_conn);
            _contactInterestAdapter = new ContactInterestAdapter(_conn);
            _membershipTypeAdapter = new MembershipTypeAdapter(_conn);
            _mailFormatAdapter = new MailFormatAdapter(_conn);
        }

        #region Fields
        private readonly ContactAdapter _adapter;
        private readonly TitleAdapter _titleAdapter;
        private readonly MaritalStatusAdapter _maritalStatusAdapter;
        private readonly HomeTownAdapter _homeTownAdapter;
        private readonly ContactInterestAdapter _contactInterestAdapter;
        private readonly MembershipTypeAdapter _membershipTypeAdapter;
        private readonly MailFormatAdapter _mailFormatAdapter;
        private readonly Connection _conn;

        #endregion

        #region Properties
        
        #endregion

        #region Methods

        public List<IDictionaryItem> LoadContactInterestsOfContact(int contactID)
        {
            return _contactInterestAdapter.LoadContactInterestsOfContact(contactID).Cast<IDictionaryItem>().ToList();
        }

        public void SaveContactInterestOfContact(int contactID, List<int> contactInterestIDs)
        {
            _contactInterestAdapter.SaveContactInterestsOfContact(
                contactID, contactInterestIDs);
        }

        public bool ContactExists(
            bool isNew, int contactID, int titleID, string firstName, string lastName, DateTime dateOfBirth)
        {
            int count;
            return
            _adapter.QueryContacts(
                new ContactQueryParameters()
                {
                    FirstNameContains = firstName,
                    LastNameContains = lastName,
                }, out count)
                .Any(
                    c => c.FirstName.Equals(firstName, StringComparison.CurrentCultureIgnoreCase) &&
                        c.LastName.Equals(lastName, StringComparison.CurrentCultureIgnoreCase) &&
                        (!c.DateOfBirth.HasValue || c.DateOfBirth.Value == dateOfBirth) &&
                        c.TitleID == titleID &&
                        (isNew || c.ContactID != contactID));
        }

        public string SexName(bool isMale)
        {
            if (isMale)
                return "Male";
            else
                return "Female";
        }

        public void SaveContact(IContact contact, bool isNew)
        {
            _adapter.Save((Contact)contact, isNew);
        }

        public IContact LoadContact(int contactID)
        {
            return _adapter.Load(contactID);
        }

        public IContact CreateNewContact()
        {
            return new Contact()
            {
                MaritalStatus = _maritalStatusAdapter.GetIDForUnspecified(),
                HomeTown = _homeTownAdapter.GetIDForUnspecified(),
                ContactInterest = _contactInterestAdapter.GetIDForUnspecified(),
                MembershipType = _membershipTypeAdapter.GetIDForUnspecified(),
                MailFormat = _mailFormatAdapter.GetIDForUnspecified()
            };
        }

        public string TitleName(int titleID)
        {
            return _titleAdapter.GetItem(titleID).Name;
        }

        public List<IDictionaryItem> AllTitles()
        {
            return _titleAdapter.GetItems().Cast<IDictionaryItem>().ToList();
        }

        public IEnumerable<IDictionaryItem> GetMaritalStatuses()
        {
            return _maritalStatusAdapter.GetMaritalStatuses().Cast<IDictionaryItem>();
        }

        public IEnumerable<IDictionaryItem> GetHomeTowns()
        {
            return _homeTownAdapter.GetItems().Cast<IDictionaryItem>();
        }

        public IEnumerable<IDictionaryItem> GetContactInterests()
        {
            return _contactInterestAdapter.GetItems().Cast<IDictionaryItem>();
        }

        public IEnumerable<IDictionaryItem> GetMembershipTypes()
        {
            return _membershipTypeAdapter.GetGetMembershipTypes().Cast<IDictionaryItem>();
        }

        public IEnumerable<IDictionaryItem> GetMailFormats()
        {
            return this._mailFormatAdapter.GetMailFormats().Cast<IDictionaryItem>();
        }

        public string GetMaritalStatusName(int id)
        {
            return _maritalStatusAdapter.GetName(id);
        }

        public string GetHomeTownName(int id)
        {
            return _homeTownAdapter.GetName(id);
        }

        public string GetContactInterestName(int id)
        {
            return _contactInterestAdapter.GetName(id);
        }

        public string GetMembershipTypeName(int id)
        {
            return _membershipTypeAdapter.GetName(id);
        }

        public string GetMailFormatName(int id)
        {
            return _mailFormatAdapter.GetName(id);
        }

        #endregion Methods

        #region Data Sources

        #endregion

    }
}
